<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Think\Page;

/**
 * 文章控制器
 * @author jry <598821125@qq.com>
 */
class PostController extends AdminController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    protected $postTable = 'Admin/Post';
    public function index()
    {
        // 搜索
        $keyword         = I('keyword', '', 'string');
        $condition       = array('like', '%' . $keyword . '%');
        $map['id|title'] = array($condition, $condition, '_multi' => true);
        
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');

        // 获取所有分类
        $p = $_GET["p"] ?: 1;
        if (I('cid')) {
            $cid = $map['cid'] = I('cid');
        }

        if ($user_group==2) {
            $map['author_id'] = $uid;   
        }
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $post_object   = D('Admin/Post');
        $data_list     = $post_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('sort desc,id desc')
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );

        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('文章列表') // 设置页面标题
            ->addTopButton('self', array( // 添加返回按钮
                'title'   => '<i class="fa fa-reply"></i> 返回分类列表',
                'class'   => 'btn btn-warning',
                'href' => U('Admin/Nav/index'))
            )
            ->addTopButton('addnew', array('href' => U('add', array('cid' => $cid)))) // 添加新增按钮
            ->addTopButton('resume') // 添加启用按钮
            ->addTopButton('forbid') // 添加禁用按钮
            ->setSearch('请输入ID/标题', U('index'))
            ->addTableColumn('id', 'ID')
            ->addTableColumn('cover', '封面', 'picture')
            ->addTableColumn('title', '标题')
            ->addTableColumn('create_time', '创建时间', 'time')
            ->addTableColumn('sort', '排序')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list) // 数据列表
            ->setTableDataPage($page->show()) // 数据列表分页
            ->addRightButton('edit') // 添加编辑按钮
            ->addRightButton('forbid') // 添加禁用/启用按钮
            ->addRightButton('delete') // 添加删除按钮
            ->display();
    }

    public function kolAddForm($cid) {

        $builder = new \Common\Builder\FormBuilder();

        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');


        $form_data['cid'] = $cid;
        $form_data['author_id'] = $uid;
        if ($user_group!=2) {
            $builder->addFormItem('kolid', 'text', 'KOL编号', 'KOL编号');
        }else{
            $builder->addFormItem('kolid', 'hidden', 'KOL编号', 'KOL编号');
            $form_data['kolid'] = $uid;
        }

        $builder->setMetaTitle('新增文章') // 设置页面标题
            ->setPostUrl(U('add')) // 设置表单提交地址
            ->addFormItem('cid', 'hidden', '分类', '分类')
            ->addFormItem('cover', 'picture', 'KOL页面头图', 'KOL页面头图')
            ->addFormItem('title', 'text', '标题', '标题')
            ->addFormItem('abstract', 'textarea', 'KOL简介', 'KOL简介')
            ->addFormItem('memo', 'textarea', '体验内容', '体验内容')
            ->addFormItem('content', 'kindeditor', 'KOL图片', 'KOL图片')  
            ->addFormItem('menu', 'kindeditor', 'KOL作品', 'KOL作品')          
            ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
            ->addFormItem('author_id', 'hidden', '作者', '作者')
            ->setFormData($form_data)
            ->display();
    }

    public function kolEditForm($id,$data) {
        $builder = new \Common\Builder\FormBuilder();
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');

        if ($user_group!=2) {
            $builder->addFormItem('kolid', 'text', 'KOL编号', 'KOL编号',array(),'','readonly=true');

        }
        $builder->setMetaTitle('新增文章') // 设置页面标题
            ->setPostUrl(U('edit')) // 设置表单提交地址
            ->addFormItem('id', 'hidden', 'ID', 'ID')
            ->addFormItem('cid', 'hidden', '分类', '分类')
            ->addFormItem('cover', 'picture', 'KOL页面头图', 'KOL页面头图')
            ->addFormItem('title', 'text', '标题', '标题')
            ->addFormItem('abstract', 'textarea', 'KOL简介', 'KOL简介')
            ->addFormItem('memo', 'textarea', '体验内容', '体验内容')
            ->addFormItem('content', 'kindeditor', 'KOL图片', 'KOL图片')
            ->addFormItem('menu', 'kindeditor', 'KOL作品', 'KOL作品')   
            ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
            ->setFormData($data)
            ->display();
    }


    public function profile()
    {
        $uid = session('user_auth.uid');

        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');
        if ($user_group==2) {
            $map['kolid'] = $uid;
            $map['cid'] = 22;
            $hasid = D('Admin/Post')->where($map)->getField('id');
            if ($hasid) {
                $this->edit($hasid);
            }else{
                $this->add(22);
            }
        }else{
            $this->redirect('/Admin/Post/index',array('cid'=>22));
        }
    }
    /**
     * 新增文档
     * @author jry <598821125@qq.com>
     */
    public function add($cid)
    {
        $uid = session('user_auth.uid');
        $user_group   = D('Admin/Access')->getFieldByUid($uid, 'group');
        if (IS_POST) {
            $post_object = D('Admin/Post');
            $data        = $post_object->create(format_data());
            if ($data) {
                if ($data['cid']==22 && $data['kolid']>0) {
                    $map['kolid'] = $data['kolid'];
                    $map['cid'] = 22;
                    $hasid = D('Admin/Post')->where($map)->getField('id');
                    if ($hasid) {
                        $this->error('KOL资料已存在，不能重复添加');
                    }
                }
                

                $id = $post_object->add();
                if ($id) {
                    $this->success('新增成功', U('index', array('cid' => $cid)));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            if ($cid==22) {
                $this->kolAddForm($cid);
                exit;
            }
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();

            


            $form_data['cid'] = $cid;
            $form_data['author_id'] = $uid;
        

            if ($user_group!=2) {
                $builder->addFormItem('kolid', 'text', 'KOL编号', 'KOL编号');
            }else{
                $builder->addFormItem('kolid', 'hidden', 'KOL编号', 'KOL编号');
                $form_data['kolid'] = $uid;
            }

            $builder->setMetaTitle('新增文章') // 设置页面标题
                ->setPostUrl(U('add')) // 设置表单提交地址
                ->addFormItem('cid', 'hidden', '分类', '分类')
                ->addFormItem('cover', 'picture', '封面', '封面')
                ->addFormItem('title', 'text', '标题', '标题')
                ->addFormItem('subtitle', 'text', '副标题', '副标题')
                ->addFormItem('abstract', 'textarea', '体验场简介', '体验场简介')
                ->addFormItem('memo', 'textarea', '体验内容', '体验内容')
		/*
                ->addFormItem('content', 'kindeditor', '体验场其他图片', '其他图片')
                */
		->addFormItem('number', 'num', '席位', '席位')
                ->addFormItem('price', 'num', '私享价', '私享价')
                ->addFormItem('comboprice', 'textarea', '套餐价', '套餐价')
                ->addFormItem('address', 'text', '地址', '地址')
                ->addFormItem('create_time', 'datetime', '体验时间', '体验时间')
                ->addFormItem('menu', 'kindeditor', '课程内容', '菜单图片及文字')
                /*
		->addFormItem('menu', 'kindeditor', '菜单图片', '菜单图片及文字')
                ->addFormItem('lead', 'picture', '讲师图片', '讲师图片')
                ->addFormItem('leadintro', 'textarea', '讲师介绍', '讲师介绍')
                ->addFormItem('leadmemo', 'kindeditor', '讲师其他资料', '讲师其他资料')
                */
                ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
                ->addFormItem('author_id', 'hidden', '作者', '作者')
                ->setFormData($form_data)
                ->display();
        }
    }

    /**
     * 编辑文章
     * @author jry <598821125@qq.com>
     */
    public function edit($id)
    {
        if (IS_POST) {
            $post_object = D('Admin/Post');
            $data        = $post_object->create(format_data());
            if ($data) {
                $id = $post_object->save();
                if ($id !== false) {
                    $this->success('更新成功', U('index', array('cid' => $data["cid"])));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($post_object->getError());
            }
        } else {
            $data = D('Admin/Post')->find($id);

            if ($data["cid"]==22) {
                $this->kolEditForm($id,$data);
                exit;
            }
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑文章') // 设置页面标题
                ->setPostUrl(U('edit')) // 设置表单提交地址
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('cid', 'hidden', '分类', '分类')
                ->addFormItem('cover', 'picture', '封面', '封面')
                ->addFormItem('title', 'text', '标题', '标题')
                ->addFormItem('kolid', 'text', 'KOL编号', 'KOL编号')
                ->addFormItem('subtitle', 'text', '副标题', '副标题')
                ->addFormItem('abstract', 'textarea', '体验场简介', '体验场简介')
                ->addFormItem('memo', 'textarea', '体验内容', '体验内容')
               /* ->addFormItem('content', 'kindeditor', '体验场其他图片', '其他图片')*/
                ->addFormItem('number', 'num', '席位', '席位')
                ->addFormItem('price', 'num', '私享价', '私享价')
                /*->addFormItem('comboprice', 'textarea', '套餐价', '套餐价')*/
                ->addFormItem('address', 'text', '地址', '地址')
                ->addFormItem('create_time', 'datetime', '体验时间', '体验时间')
		->addFormItem('menu', 'kindeditor', '课程内容', '课程图片及文字')
                /*
		->addFormItem('menu', 'kindeditor', '菜单图片', '菜单图片及文字')
                ->addFormItem('lead', 'picture', '讲师图片', '讲师图片')
                ->addFormItem('leadintro', 'textarea', '讲师介绍', '讲师介绍')
                ->addFormItem('leadmemo', 'kindeditor', '讲师其他资料', '讲师其他资料')
                */
                ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
                ->setFormData($data)
                ->display();
        }
    }
}
